const config = require('../../config');
const qiniu = require('qiniu');

const ControllerQiniu = {
    //! 七牛云上传token
    uploadToken: async function (req, res, next) {
        try {
            const accessKey = config.qiniu.accessKey;
            const secretKey = config.qiniu.secretKey;
            const bucket = config.qiniu.options.bucket;
            const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
            const options = { scope: bucket };
            const domain = config.qiniu.options.domain;

            const putPolicy = new qiniu.rs.PutPolicy(options);
            const uploadToken = putPolicy.uploadToken(mac);

            res.json({ code: 200, data: { uploadToken, domain } })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    }
}

module.exports = ControllerQiniu;